package com.dhec.ptms.epr.db;

import java.util.ArrayList;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.dhec.ptms.epr.model.ReferBenchMark;
import com.ionglobal.db.DBI;
import com.ionglobal.lib.SysException;

public class ReferBenchMarkDB {
	
	private static final Logger logger = LoggerFactory.getLogger(ReferBenchMarkDB.class);

	public List<ReferBenchMark> list(DBI dbi, int page, int pagesize){
		ArrayList<ReferBenchMark> list = new ArrayList<ReferBenchMark>(0);
		try {
			dbi.skipToPage(pagesize, page, "select * from EPR_ReferBenchMark order by id desc");
			while(dbi.rsNext()){
				ReferBenchMark bean = new ReferBenchMark();
				this.handleData(dbi, bean);
				list.add(bean);
			}
			dbi.rsClose();
		} catch (SysException e) {
			logger.error("ReferBenchMarkDB.list(DBI) Error!", e);
		}
		
		return list;
		
	}
	
	public long count(DBI dbi){
		long c = 0;
		try {
			dbi.execQuery("select count(*) from EPR_ReferBenchMark");
			if(dbi.rsNext()){
				c = dbi.rsGetLong(1);
			}
			dbi.rsClose();
		} catch (SysException e) {
			logger.error("ReferBenchMarkDB.list(DBI) Error!", e);
		}
		
		return c;
		
	}
	
	public void handleData(DBI dbi, ReferBenchMark bean) throws SysException{
		bean.setCreatetime(dbi.rsGetDate("createtime"));
		bean.setCreateuser(dbi.rsGetString("createuser"));
		bean.setEnabled(dbi.rsGetInt("enabled")==1);
		bean.setId(dbi.rsGetInt("id"));
		bean.setEngineno(dbi.rsGetString("engineno"));
		bean.setSpec(dbi.rsGetString("spec"));
		bean.setUpdatetime(dbi.rsGetDate("updatetime"));
		bean.setUpdateuser(dbi.rsGetString("updateuser"));
	}
	
	
	public ReferBenchMark find(DBI dbi, int id){
		ReferBenchMark bean = null;
		try {
			dbi.execQuery("select * from EPR_ReferBenchMark where id="+id);
			if(dbi.rsNext()){
				bean = new ReferBenchMark();
				this.handleData(dbi, bean);
			}
			dbi.rsClose();
		} catch (SysException e) {
			logger.error("ReferBenchMarkDB.find(DBI, int) Error!", e);
		}
		
		return bean;
		
	}
	
	public boolean update(DBI dbi, ReferBenchMark bean){
		try {
			return dbi.execute("update EPR_ReferBenchMark set spec='"+dbi.fixSql(bean.getSpec())+"',"
					+ "engineno='"+dbi.fixSql(bean.getEngineno())+"',"
					+ "updateuser='"+dbi.fixSql(bean.getUpdateuser())+"',"
					+ "updatetime="+dbi.getCurrentDateTime()+","
					+ "enabled="+(bean.getEnabled()?"1":"0")+" where id="+bean.getId());
		} catch (SysException e) {
			logger.error("ReferBenchMarkDB.update(DBI, ReferBenchMark) Error!", e);
		}
		
		return false;
		
	}
	
	public boolean save(DBI dbi, ReferBenchMark bean){
		try {
			return dbi.execute("insert into EPR_ReferBenchMark (id, spec, engineno, createuser, createtime, updateuser, updatetime, enabled) values("+bean.getId()+","
					+ "'"+dbi.fixSql(bean.getSpec())+"',"
					+ "'"+dbi.fixSql(bean.getEngineno())+"',"
					+ "'"+dbi.fixSql(bean.getCreateuser())+"',"
					+ ""+dbi.getCurrentDateTime()+","
					+ "'"+dbi.fixSql(bean.getUpdateuser())+"',"
					+ ""+dbi.getCurrentDateTime()
					+","+(bean.getEnabled() ? "1": "0")+")");
		} catch (SysException e) {
			logger.error("ReferBenchMarkDB.save(DBI, ReferBenchMark) Error!", e);
		}
		
		return false;
		
	}
	
	public boolean delete(DBI dbi, int id){
		try {
			return dbi.execute("delete from EPR_ReferBenchMark where id="+id);
		} catch (SysException e) {
			logger.error("ReferBenchMarkDB.delete(DBI, ReferBenchMark) Error!", e);
		}
		
		return false;
		
	}
}
